package com.elinkway.infinitemovies.share;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.ShareContent;
import com.elinkway.infinitemovies.bean.VideoDataBean;
import com.elinkway.infinitemovies.bigdata.RealDataApi;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.ui.activity.commonwebview.CommonWebViewActivity;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivityFroWebView;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.ReportUtil;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.le123.ysdq.R;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class SharePopupWindow {

    private final static String TAG = "SharePopupWindow";

    private final int FROM_VIDEO_DETAIL_ACTIVITY = 1;

    private final int FROM_COMMON_WEBVIEW_ACTIVITY = 2;

    private final int FROM_PLAY_WEBVIEW_ACTIVITY = 3;

    private final int SHARE_WEIXIN = 0;

    private PopupWindow mSharePopWindow;

    private View mPopWindowView;

    private Activity mContext;

    private ShareOnClickListener mShareOnClickListener;

    private RelativeLayout mShareWeixin;

    private RelativeLayout mShareFirend;

    private RelativeLayout mShareWeiBo;

    private RelativeLayout mShareQQ;

    private RelativeLayout mShareQQZone;

    private RelativeLayout mCancelIv;

    private TextView mTitleTv;

    private String shareLink;

    private String mSharePoster = "http://i0.letvimg.com/lc05_isvrs/201605/25/19/28/2f1a77bf-9619-40e1-9a99-39977a6b95cf.jpg";

    private int mPopFrom;  // 目前1代表从VideoDetailActivity 弹, 2代表从CommonWebViewActivity弹

    private RelativeLayout bg;

    public SharePopupWindow(Activity activity, int popFrom) {
        mContext = activity;
        mPopFrom = popFrom;
        mShareOnClickListener = new ShareOnClickListener();
    }

    /**
     * 获取POPupWindow的对象
     *
     * @return zhangshuo
     * 2014年5月30日 下午4:02:18
     */
    public PopupWindow initPopWindow() {
        LayoutInflater inflater = LayoutInflater.from(mContext);

        // 加载筛选弹出的Popwind模板
        mPopWindowView = inflater.inflate(R.layout.video_detial_share, null);
        mSharePopWindow = new PopupWindow(mPopWindowView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        mSharePopWindow.setAnimationStyle(R.style.sharepopupAnimation);
        if (null != mSharePopWindow) {
            mSharePopWindow.setFocusable(false);
            // 用于POPwindow点击消失
            mSharePopWindow.setBackgroundDrawable(new BitmapDrawable());
            mSharePopWindow.setOutsideTouchable(false);
            mSharePopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        }
        initView();
        setListener();
        return mSharePopWindow;
    }

    private void initView() {
        mShareWeixin = (RelativeLayout) mPopWindowView
                .findViewById(R.id.weixinshare);
        mShareFirend = (RelativeLayout) mPopWindowView
                .findViewById(R.id.friendshare);
        mShareWeiBo = (RelativeLayout) mPopWindowView
                .findViewById(R.id.sinashare);
        mShareQQ = (RelativeLayout) mPopWindowView.findViewById(R.id.qqshare);
        mShareQQZone = (RelativeLayout) mPopWindowView.findViewById(R.id.qq_zone);
        mCancelIv = (RelativeLayout) mPopWindowView.findViewById(R.id.right_close_iv);
        mTitleTv = (TextView) mPopWindowView.findViewById(R.id.title_tv);
        mTitleTv.setText("分享");
    }

    private void setListener() {
        mShareWeixin.setOnClickListener(mShareOnClickListener);
        mShareFirend.setOnClickListener(mShareOnClickListener);
        mShareWeiBo.setOnClickListener(mShareOnClickListener);
        mShareQQ.setOnClickListener(mShareOnClickListener);
        mShareQQZone.setOnClickListener(mShareOnClickListener);
        mCancelIv.setOnClickListener(mShareOnClickListener);
    }

    private class ShareOnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {

            ShareContent content = new ShareContent();
            String videoTitle = "";
            JSONObject jsonObject=new JSONObject();
            if (mPopFrom == FROM_VIDEO_DETAIL_ACTIVITY) {
                videoTitle = ((VideoDetailActivity) mContext)
                        .getVideoTitle();
                content = new ShareContent();
                content.setH5Title(videoTitle);
                content.setLink(getShareLink());
                content.setImageUrl(mSharePoster);
                content.setDbMap(getShareDataMap());
            } else if (mPopFrom == FROM_COMMON_WEBVIEW_ACTIVITY) {
                videoTitle = ((CommonWebViewActivity) mContext)
                        .getShareTitle();
                content = new ShareContent();
                content.setH5Title(((CommonWebViewActivity) mContext).getVideoTitle());
                content.setLink(((CommonWebViewActivity) mContext).getLoadUrl());
                content.setImageUrl(getSharePic());
            } else if(mPopFrom == FROM_PLAY_WEBVIEW_ACTIVITY){
                content = new ShareContent();
                content.setH5Title(((PlayActivityFroWebView) mContext).getShareTitle());
                content.setLink(((PlayActivityFroWebView) mContext).getShareUrl());
                content.setImageUrl(((PlayActivityFroWebView) mContext).getSharePic());
                addCommonData(jsonObject);
            }
            VideoDataBean video = getVideo();

            switch (v.getId()) {
                case R.id.weixinshare:
                    //TODO 微信分享
                    addReferValue(jsonObject, "SHORTCUT_WEIXIN_FRIENDS");
                    if (mPopFrom == 1) {
                        content.setTitle(getWeiXinShareTitle());
                    } else if (mPopFrom == 2) {
                        content.setTitle((videoTitle));
                    }
                    if (mContext instanceof CommonWebViewActivity &&
                            !TextUtils.isEmpty(((CommonWebViewActivity) mContext).getShareDesc())) {
                        content.setContent(((CommonWebViewActivity) mContext).getShareDesc());
                    } else if (mContext instanceof PlayActivityFroWebView) {
                        content.setTitle(((PlayActivityFroWebView) mContext).getShareTitle());
                        content.setContent(((PlayActivityFroWebView) mContext).getShareDesc());
                    } else {
                        content.setContent(getQzoneTitle(videoTitle));
                    }
                    ShareUtils.shareToThird(mContext, content, SHARE_MEDIA.WEIXIN);
                    CloudShareReport("cloudshareweixin");
                    if (video != null) {
                        RealDataApi.RealDataShareReport(video.getVid(), video.getAid(), video.getCategory(), video.getSeid(), video.getBucket());
                    }
                    break;
                case R.id.friendshare:
                    //TODO 	朋友圈分享
                    addReferValue(jsonObject, "SHORTCUT_WEIXIN_MOMENTS");
                    LogUtils.e("share", "share to friend");
                    if (mContext instanceof CommonWebViewActivity &&
                            !TextUtils.isEmpty(((CommonWebViewActivity) mContext).getShareDesc())) {
                        content.setContent(((CommonWebViewActivity) mContext).getShareDesc());
                        content.setTitle(((CommonWebViewActivity) mContext).getShareDesc());
                    } else if (mContext instanceof PlayActivityFroWebView) {
                        content.setTitle(((PlayActivityFroWebView) mContext).getShareTitle());
                        content.setContent(((PlayActivityFroWebView) mContext).getShareDesc());
                    } else {
                        content.setTitle(getFriendsTitile(videoTitle));
                        content.setContent(getFriendsTitile(videoTitle));
                    }
                    LogUtils.e("share", "share to friend " + content.getContent());
                    ShareUtils.shareToThird(mContext, content, SHARE_MEDIA.WEIXIN_CIRCLE);
                    CloudShareReport("cloudshareweixinmoments");
                    if (video != null) {
                        RealDataApi.RealDataShareReport(video.getVid(), video.getAid(), video.getCategory(), video.getSeid(), video.getBucket());
                    }
                    break;
                case R.id.sinashare:
                    addReferValue(jsonObject, "SHORTCUT_WEIBO");
                    content.setTitle(videoTitle);
                    if (mContext instanceof CommonWebViewActivity &&
                            !TextUtils.isEmpty(((CommonWebViewActivity) mContext).getShareDesc())) {
                        content.setContent(((CommonWebViewActivity) mContext).getShareDesc());
                    } else if (mContext instanceof PlayActivityFroWebView) {
                        content.setTitle(((PlayActivityFroWebView) mContext).getShareTitle());
                        content.setContent(((PlayActivityFroWebView) mContext).getShareTitle());
                    } else {
                        content.setContent(getQzoneTitle(videoTitle));
                    }
                    ShareUtils.shareToThird(mContext, content, SHARE_MEDIA.SINA);
                    //TODO 微博分享
                    CloudShareReport("cloudshareweibo");
                    if (video != null) {
                        RealDataApi.RealDataShareReport(video.getVid(), video.getAid(), video.getCategory(), video.getSeid(), video.getBucket());
                    }
                    break;
                case R.id.qqshare:
                    //TODO qq分享
                    addReferValue(jsonObject, "SHORTCUT_QQ");
                    LogUtils.e("qq", "share to qq");
                    content.setTitle(videoTitle);
                    if (mContext instanceof CommonWebViewActivity &&
                            !TextUtils.isEmpty(((CommonWebViewActivity) mContext).getShareDesc())) {
                        content.setContent(((CommonWebViewActivity) mContext).getShareDesc());
                    } else if (mContext instanceof PlayActivityFroWebView) {
                        content.setTitle(((PlayActivityFroWebView) mContext).getShareTitle());
                        content.setContent(((PlayActivityFroWebView) mContext).getShareDesc());
                    } else {
                        content.setContent(getQzoneTitle(videoTitle));
                    }
                    ShareUtils.shareToThird(mContext, content, SHARE_MEDIA.QQ);
                    UmengEventPoster.postH5Share(content.getLink(), content.getH5Title(), "qq", "unknow", "events");
                    CloudShareReport("cloudshareqq");
                    if (video != null) {
                        RealDataApi.RealDataShareReport(video.getVid(), video.getAid(), video.getCategory(), video.getSeid(), video.getBucket());
                    }
                    break;
                case R.id.qq_zone:
                    addReferValue(jsonObject, "SHORTCUT_QQ_SPACE");
                    LogUtils.e("qq", "share to qqzone");
                    content.setTitle(videoTitle);
                    if (mContext instanceof CommonWebViewActivity &&
                            !TextUtils.isEmpty(((CommonWebViewActivity) mContext).getShareDesc())) {
                        content.setContent(((CommonWebViewActivity) mContext).getShareDesc());
                    } else if (mContext instanceof PlayActivityFroWebView) {
                        content.setTitle(((PlayActivityFroWebView) mContext).getShareTitle());
                        content.setContent(((PlayActivityFroWebView) mContext).getShareDesc());
                    } else {
                        content.setContent(getQzoneTitle(videoTitle));
                    }
                    ShareUtils.shareToThird(mContext, content, SHARE_MEDIA.QZONE);
                    UmengEventPoster.postH5Share(content.getLink(), content.getH5Title(), "qzone", "unknow", "events");
                    CloudShareReport("cloudshareqqzone");
                    if (video != null) {
                        RealDataApi.RealDataShareReport(video.getVid(), video.getAid(), video.getCategory(), video.getSeid(), video.getBucket());
                    }
                    break;
                case R.id.right_close_iv:
                    break;
                default:
                    break;
            }

            //只有一点资讯过来的才上报
            if (mPopFrom == FROM_PLAY_WEBVIEW_ACTIVITY) {
                ReportUtil.report(jsonObject);
            }
            dissMissPopupWindow();
        }
    }

    private JSONObject addCommonData(JSONObject jsonObject){
        try {
            jsonObject.put("userid", ((PlayActivityFroWebView) mContext).getUserid());
            jsonObject.put("pageid", ((PlayActivityFroWebView) mContext).getPageid());
            jsonObject.put("impid", ((PlayActivityFroWebView) mContext).getImpid());
            jsonObject.put("docid", ((PlayActivityFroWebView) mContext).getDocid());
            jsonObject.put("subType", "shareVideo");
            jsonObject.put("srcType", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    private JSONObject addReferValue(JSONObject jsonObject, String value){
        try {
            jsonObject.put("refer_social", value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    private String getWeiXinShareTitle() {
        if (null != mContext) {
            return mContext.getString(R.string.weixinsharetitle);
        }
        return "";
    }

    private String getFriendsTitile(String name) {
        StringBuffer titleBuff = new StringBuffer();
        titleBuff.append(mContext.getString(R.string.weixinfriends1));
        titleBuff.append("《");
        titleBuff.append(name);
        titleBuff.append("》");
        titleBuff.append(",");
        titleBuff.append(mContext.getString(R.string.weixinfriends2));
        return titleBuff.toString();
    }

    private String getQzoneTitle(String name) {
        StringBuffer titleBuff = new StringBuffer();
        titleBuff.append(mContext.getString(R.string.qzonshare_1));
        titleBuff.append("《");
        titleBuff.append(name);
        titleBuff.append("》");
        titleBuff.append(mContext.getString(R.string.qzoneshare_2));
        return titleBuff.toString();
    }

    private String getShareLink() {
        try {
            if (mContext instanceof VideoDetailActivity) {
                return ((VideoDetailActivity) mContext).getShareLink();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getSharePic() {
        try {
            if (mContext instanceof CommonWebViewActivity) {
                return ((CommonWebViewActivity) mContext).getSharePic();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private Map<String, String> getShareDataMap() {
        try {
            if (mContext instanceof VideoDetailActivity) {
                return ((VideoDetailActivity) mContext).getShareDBMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private VideoDataBean getVideo() {
        try {
            if (mContext instanceof VideoDetailActivity) {
                return ((VideoDetailActivity) mContext).getVideo();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean isShowing() {
        if (null != mSharePopWindow && mSharePopWindow.isShowing()) {
            return true;
        }
        return false;
    }

    public void dissMissPopupWindow() {
        if (null != mSharePopWindow && mSharePopWindow.isShowing()) {
            mSharePopWindow.dismiss();
            if (bg != null) {
                UIs.showOrHideBg(bg, false);
            }
        }
    }

    /**
     * 云盘分享上报
     */
    private void CloudShareReport(String actionName) {
        if (mPopFrom == 1) {
            VideoDetailActivity detailActivity = (VideoDetailActivity) mContext;
            if ("nets".equals(detailActivity.getVideo().getSubsrc())) {
                String name = detailActivity.getVideoTitle();
                UmengEventPoster.postCloudShare(name, actionName);
            }
        }
    }

    public String getmSharePoster() {
        return mSharePoster;
    }

    public void setmSharePoster(String mSharePoster) {
        this.mSharePoster = mSharePoster;
    }

    public RelativeLayout getBg() {
        return bg;
    }

    public void setBg(RelativeLayout bg) {
        this.bg = bg;
    }
}
